package edu.udel.mboch.towerdefense;

import java.util.ArrayList;

public class BattleGrid extends Grid
{
	private static final long serialVersionUID = 608430037752373471L;
	private Position spawn2;
	private Position end2;
	private ArrayList<PathPosition> path2;
	
	public BattleGrid(Position spawn, Position spawn2, Position end, Position end2, Terrain[][] terrainGrid)
	{
		super(spawn, end, terrainGrid);
		this.end2 = end2;
		this.spawn2 = spawn2;
		path2 = findPathPoints2();
	}
	
	public BattleGrid(Tower[][] towerGrid, Mob[][] mobGrid, Terrain[][] terrainGrid, Position spawn, Position spawn2, Position end, Position end2, ArrayList<PathPosition> path, ArrayList<PathPosition> path2, ArrayList<Mob> mobs, ArrayList<Projectile> projectiles)
	{
		super(towerGrid, mobGrid, terrainGrid, spawn, end, path, mobs, projectiles);
		this.end2 = end2;
		this.spawn2 = spawn2;
		this.path2 = path2;
	}
	
	private ArrayList<PathPosition> findPathPoints2()
	{
		ArrayList<PathPosition> allPos = new ArrayList<PathPosition>();
		allPos.add(new PathPosition(end2.x(), end2.y(), 0));
		int count = 1;
		int i = 0;
		PathPosition currentPos = allPos.get(0);
		while(currentPos.x() != spawn2.x() || currentPos.y() != spawn2.y())
		{
			ArrayList<PathPosition> newPoses = new ArrayList<PathPosition>();
			int x = currentPos.x();
			int y = currentPos.y();
			if(x+1 < MAX_X && !terrainGrid[y][x+1].isBuildable())
				newPoses.add(new PathPosition(x+1, y, count));
			if(x-1 >= 0 && !terrainGrid[y][x-1].isBuildable())
				newPoses.add(new PathPosition(x-1, y, count));
			if(y+1 < MAX_Y && !terrainGrid[y+1][x].isBuildable())
				newPoses.add(new PathPosition(x, y+1, count));
			if(y-1 >= 0 && !terrainGrid[y-1][x].isBuildable())
				newPoses.add(new PathPosition(x, y-1, count));
			count++;
			ArrayList<PathPosition> newPosesCopy = new ArrayList<PathPosition>();
			for(PathPosition p1 : newPoses)
			{
				newPosesCopy.add(p1);
				for(PathPosition p2 : allPos)
					if(p1.x() == p2.x() && p1.y() == p2.y() && p2.getCount() <= p1.getCount())
						newPosesCopy.remove(p1);
			}
			newPoses = newPosesCopy;
			for(PathPosition p : newPoses)
				allPos.add(p);
			currentPos = allPos.get(++i);
		}
		
		ArrayList<PathPosition> path = new ArrayList<PathPosition>();
		path.add(new PathPosition(spawn2.x(), spawn2.y(), count));
		currentPos = path.get(0);
		while(!currentPos.equals(allPos.get(0)))
		{
			PathPosition nextPos = new PathPosition(currentPos.x(), currentPos.y(), currentPos.getCount());
			for(PathPosition p : allPos)
			{
				if(currentPos.isNextTo(p) && p.getCount() < nextPos.getCount())
					nextPos = p;	
			}
			currentPos = nextPos;
			path.add(currentPos);
		}
		path.remove(0);
		return path;
	}
	
	public ArrayList<PathPosition> getPath2()
	{
		return path2;
	}
	
	public Position getEnd2()
	{
		return end2;
	}
	
	public Position getSpawn2()
	{
		return spawn2;
	}

	public BattleGrid copy()
	{
		Tower[][] tCopy = new Tower[MAX_Y][MAX_X];
		Mob[][] mCopy = new Mob[MAX_Y][MAX_X];
		for(int i = 0; i<MAX_Y; i++)
			for(int j = 0; j<MAX_X; j++)
			{
				if(towerGrid[i][j] != null)
					tCopy[i][j] = towerGrid[i][j].copy();
				if(mobGrid[i][j] != null)
					mCopy[i][j] = mobGrid[i][j].copy();
			}
		ArrayList<Mob> mobCopy = new ArrayList<Mob>();
		for(Mob m : mobs)
			mobCopy.add(m.copy());
		ArrayList<Projectile> projectilesCopy = new ArrayList<Projectile>();
		for(Projectile p : projectiles)
			projectilesCopy.add(p.copy());
		return new BattleGrid(tCopy, mCopy, terrainGrid, spawn, spawn2, end, end2, path, path2, mobCopy, projectilesCopy);
	}

}
